package org.setec.manager.stubview;

import org.setec.utils.io.Persistance;
import org.setec.utils.io.Stub;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class StorageViewTransformer {



    public String getStorageView(String storage){

        StringBuffer result = new StringBuffer();

        List stubFileList = getStorageStubList(storage);
        List stubList = loadStubs(stubFileList);

        return result.toString();
    }


    public List getStorageStubs(String storage){
        List stubFileList = getStorageStubList(storage);
        return loadStubs(stubFileList);
    }


    public List getStorageStubList(String storage){
        List result = new ArrayList();
        File storageDir = new File(storage);
        if( storageDir.listFiles()==null ){
            return result;
        }
        for( Iterator it = Arrays.asList(storageDir.listFiles()).iterator(); it.hasNext(); ){
            File stubFile =(File) it.next();
            if( stubFile.isFile() ){
                result.add(stubFile.getAbsolutePath());
            }
        }
        return result;
    }

    public List loadStubs(List stubFiles){
        List result = new ArrayList();
        for( Iterator it = stubFiles.iterator(); it.hasNext(); ){
            String path = (String)it.next();
            Stub stub = Persistance.loadStub(path);
            result.add(stub);
        }
        return result;
    }
}
